package org.cn.zhaozhx.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Enumeration;
import java.util.Hashtable;

public class ManageContent {
    private int dir_deep = 0;
    private int dir_size = 0;
    //输入路径相对于根目录的深度
    private int sourcepath_deep; 
    //储存输入路径下的所有子目录，及其相对于根目录的深度keys=(String)dirPath,values=(Integer)dirDeep
    private Hashtable ht = new Hashtable(); 
    
    /**
     * @Description: 获取指定目录大小
     * @param dir_path
     * @return
     * @throws FileNotFoundException
     */
    public synchronized int getSize(String dir_path) throws FileNotFoundException {
        File dir = new File(dir_path);
        if (!dir.exists()) {
            throw new FileNotFoundException();
        }
        if (!dir.isDirectory()) {
            dir_size = (int) dir.length();
        } else {
            File[] fe = dir.listFiles();
            for (int i = 0; i < fe.length; i++) {
                if (fe[i].isDirectory()) {
                    getSize(fe[i].toString());
                } else {
                    dir_size += fe[i].length();
                }
            }
        }
        return dir_size;
    }
    
    /**
     * @Description: 删除指定目录
     * @param dir_path
     * @throws FileNotFoundException
     */
    public synchronized void deleteFiles(String dir_path) throws FileNotFoundException {
        File file = new File(dir_path);
        if (!file.exists()) {
            throw new FileNotFoundException();
        }
        if (file.isDirectory()) {
            File[] fe = file.listFiles();
            for (int i = 0; i < fe.length; i++) {
                deleteFiles(fe[i].toString());
                fe[i].delete(); //删除已经是空的子目录
            }
        }
        file.delete(); //删除总目录
    }
    
    private void allDeep(File file) {
        File[] fe = file.listFiles();
        for (int i = 0; i < fe.length; i++) {
            if (fe[i].isDirectory()) {
                allDeep(fe[i]); //取得其所有子目录的深度
                ht.put(fe[i].toString(), new Integer(count(fe[i].toString()) - sourcepath_deep));
            }
        }
    }
    
    public synchronized int getDeep(String dir_path) throws FileNotFoundException {
        File file = new File(dir_path);
        if (!file.exists()) {
            throw new FileNotFoundException();
        }
        if (!file.isDirectory()) {
            return -1;//输入路径不是目录时就返回-1
        }
        sourcepath_deep = count(file.toString());
        allDeep(file);
        Enumeration enumeration = ht.keys();
        int max = 0;
        String keys;
        Integer values;
        while (enumeration.hasMoreElements()) {
            keys = (String) enumeration.nextElement();
            values = (Integer) ht.get(keys);
            if (values.intValue() > max)
                max = values.intValue();
        }
        return max;
    }
    
    private int count(String str) {
        String regEx = "/";
        if (System.getProperty("file.separator").equals("\\"))//windows和unix下的路径分割符不一样
            regEx = "\\\\";
        return str.split(regEx).length;
    }
}
